-module(bnode_api).
-include("btree.hrl").

%% API
-export([forward/2]).
-export([backward/2]).

forward(BTree, BNode) when is_record(BNode, bn) ->
    #bn{handler = Handler} = BNode,
    Res = Handler:forward(BTree, BNode),
    Res;
forward(#bt{nodes = Nodes} = BTree, NodeId) ->
    case maps:get(NodeId, Nodes) of
        #bn{} = BNode ->
            forward(BTree, BNode);
        _ ->    % 没有节点信息
            %?ERR("NodeId ~w not in nodes:~w",[NodeId, BTree#bt.name]),
            BTree
    end.

backward(#bt{nodes = Nodes} = BTree, #bn{parent = Parent} = _BNode) ->
    case maps:find(Parent, Nodes) of
        {ok, #bn{handler = Handler} = ParentNode} ->
            Handler:backward(BTree, ParentNode);
        _ ->
            BTree
    end.
